package controller;

import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") CommonsMultipartFile file, HttpSession session) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());

//        file.getInputStream()

//        file.transferTo(new File("e:/upload",file.getOriginalFilename()));

//        String uploadDir=session.getServletContext().getRealPath("WEB-INF/upload");



        String rootPath=this.getClass().getClassLoader().getResource("").getPath();
        System.out.println(rootPath);
        String uploadDir=rootPath.substring(0,rootPath.lastIndexOf("/WEB-INF"))+"/WEB-INF/upload";


        System.out.println(uploadDir);
        file.transferTo(new File(uploadDir,file.getOriginalFilename()));

        return "upload";
    }
}
